from tkinter import Tk, messagebox
import socket
from boot import sys


__all__ = ["qq9_app_lock"]


def _error_message_box(message: str):
    root = Tk()
    root.withdraw()  # 隐藏主窗口
    messagebox.showerror("错误", message)
    root.quit()


def qq9_app_lock(cls):
    def wrapper(*args, **kwargs):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.bind(("127.0.0.1", 53332))
        except OSError:
            _error_message_box("已经打开了蓝盾记账2.0，不能重复启动")
            sys.exit(1)
        cls(*args, **kwargs)
        return cls
    return wrapper